﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyDay13.Context;
using MyDay13.Models;
using MyDay3.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyDay13.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PerLogController : ControllerBase
    {
        private readonly Day13Context db;
        public PerLogController( Day13Context dbs)
        {
          
            db = dbs;
        }
        [HttpGet]
        [Route("urshow")]
        public IActionResult rests(int id)
        {

            var list = (from u in db.D13User
                        join ur in db.D13UserRoleTable on u.UId equals ur.UserId
                        join r in db.D13Role on ur.RoleId equals r.RId
                        join rp in db.D13RolePowerTable on r.RId equals rp.RoleId
                        join p in db.D13Power on rp.PowerId equals p.PId
                        where u.UId == id
                        select p).ToList();

            return Ok(DIGUI(list, 0));

           
        }
        private List<PrwerTo> DIGUI(List<D13Power> d13Powers,int ids)
        {
            List<PrwerTo> str = new List<PrwerTo>();
            var treelist = d13Powers.Where(x => x.PIdNumber == ids).ToList();
            treelist.ForEach(x =>
            {
                PrwerTo tree = new PrwerTo();
                tree.PId = x.PId.ToString();
                tree.PIdNumber = x.PIdNumber;
                tree.PowerName = x.PowerName;
                tree.PUrl = x.PUrl;
                tree.prwerTos = DIGUI(d13Powers, x.PId);
                str.Add(tree);
            });
            return str;
        }
    }
}
